﻿#include <sb.h>

kMutex* mutex;
kCond* cond;

kpointer thd_s(kpointer)
{
	k_mesg("[s] 3초 쉼\n");
	k_ssleep(3);

	k_mesg("[s] 락얻기\n");
	k_mutex_enter(mutex);
	k_mesg("[s] 락얻음\n");

	k_mesg("[s] 컨디션 바꿀껄\n");
	k_cond_signal(cond);
	k_mesg("[s] 컨디션 바꿈\n");

	k_mesg("[s] 락풀기\n");
	k_mutex_leave(mutex);
	k_mesg("[s] 락풀음\n");

	return NULL;
}

kpointer thd_r(kpointer)
{
	k_mesg("[r]\t 락얻기\n");
	k_mutex_enter(mutex);
	k_mesg("[r]\t 락얻음\n");

	k_mesg("[r]\t 신호 기둘\n");
	k_cond_wait(cond, mutex);
	k_mesg("[r]\t 신호 옴\n");

	k_mesg("[r]\t 락풀기\n");
	k_mutex_leave(mutex);
	k_mesg("[r]\t 락풀음\n");

	return NULL;
}

int main(void)
{
	k_mesg("THREAD CONDITION TEST\n\n");

	mutex = k_mutex_new();
	cond = k_cond_new();

	kThread* tr = k_thread_new(thd_r, NULL, 0, 0);
	kThread* ts = k_thread_new(thd_s, NULL, 0, 0);

	k_thread_start(tr);
	k_thread_start(ts);

	k_thread_delete(tr);
	k_thread_delete(ts);

	k_cond_delete(cond);
	k_mutex_delete(mutex);

	return 0;
}

